package com.fly.controller;

import com.fly.domain.Role;
import com.fly.service.RoleService;
import com.fly.vo.Response;
import com.fly.web.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Fly
 * @Description 角色控制器
 * @Date Created in 8:50 2018/1/31
 * @Modified by
 */
@RestController
@RequestMapping("roles")
public class RoleController extends BaseController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping()
    public List<Role> list() {
        return roleService.findAll();
    }

    /**
     * 保存
     *
     * @param role
     * @return
     */
    @PostMapping()
    public ResponseEntity<Response> save(@RequestBody Role role) {
        Role rRole = roleService.save(role);
        return ResponseEntity.ok(new Response(true, "保存成功", rRole));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Response> remove(@PathVariable("id") Long id) {
        roleService.delete(id);
        return ResponseEntity.ok(new Response(false, "保存成功", null));
    }
}
